package me.zhengjie.modules.consultant.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import me.zhengjie.base.PageInfo;
import me.zhengjie.base.QueryHelpMybatisPlus;
import me.zhengjie.base.impl.CommonServiceImpl;
import me.zhengjie.utils.ConvertUtil;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.modules.consultant.domain.Consultant;
import me.zhengjie.modules.consultant.service.ConsultantService;
import me.zhengjie.modules.consultant.service.dto.ConsultantDto;
import me.zhengjie.modules.consultant.service.dto.ConsultantQueryParam;
import me.zhengjie.modules.consultant.service.mapper.ConsultantMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Pageable;
import java.util.*;

/**
* @author TianI
* @date 2022-01-29
*/
@Service
@AllArgsConstructor
// @CacheConfig(cacheNames = ConsultantService.CACHE_KEY)
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class ConsultantServiceImpl extends CommonServiceImpl<ConsultantMapper, Consultant> implements ConsultantService {

    // private final RedisUtils redisUtils;
    private final ConsultantMapper consultantMapper;

    @Override
    public PageInfo<ConsultantDto> queryAll(ConsultantQueryParam query, Pageable pageable) {
        IPage<Consultant> queryPage = PageUtil.toMybatisPage(pageable);
        IPage<Consultant> page = consultantMapper.selectPage(queryPage, QueryHelpMybatisPlus.getPredicate(query));
        return ConvertUtil.convertPage(page, ConsultantDto.class);
    }

    @Override
    public List<ConsultantDto> queryAll(ConsultantQueryParam query){
        return ConvertUtil.convertList(consultantMapper.selectList(QueryHelpMybatisPlus.getPredicate(query)), ConsultantDto.class);
    }

    @Override
    public Consultant getById(Long id) {
        return consultantMapper.selectById(id);
    }

    @Override
    // @Cacheable(key = "'id:' + #p0")
    public ConsultantDto findById(Long id) {
        return ConvertUtil.convert(getById(id), ConsultantDto.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(ConsultantDto resources) {
        Consultant entity = ConvertUtil.convert(resources, Consultant.class);
        return consultantMapper.insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateById(ConsultantDto resources){
        Consultant entity = ConvertUtil.convert(resources, Consultant.class);
        int ret = consultantMapper.updateById(entity);
        // delCaches(resources.id);
        return ret;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeByIds(Set<Long> ids){
        // delCaches(ids);
        return consultantMapper.deleteBatchIds(ids);
    }

    @Override
    public List<Consultant> getConsultant(Long id) {
        List<Consultant> real_estate_project_id = consultantMapper.selectList(new QueryWrapper<Consultant>().eq("real_estate_project_id", id));
        return real_estate_project_id;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeById(Long id){
        Set<Long> set = new HashSet<>(1);
        set.add(id);
        return this.removeByIds(set);
    }

    /*
    private void delCaches(Long id) {
        redisUtils.delByKey(CACHE_KEY + "::id:", id);
    }

    private void delCaches(Set<Long> ids) {
        for (Long id: ids) {
            delCaches(id);
        }
    }*/

    /*
    @Override
    public void download(List<ConsultantDto> all, HttpServletResponse response) throws IOException {
      List<Map<String, Object>> list = new ArrayList<>();
      for (ConsultantDto consultant : all) {
        Map<String,Object> map = new LinkedHashMap<>();
              map.put("置业顾问姓名", consultant.getConsultantName());
              map.put("置业顾问头像url", consultant.getConsultantAvatar());
              map.put("置业顾问手机号", consultant.getConsultantPhone());
              map.put("关联房产项目", consultant.getRealEstateProjectId());
        list.add(map);
      }
      FileUtil.downloadExcel(list, response);
    }*/
}
